package com.cpi.cornpp.plants.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.plants.exception.PlantException;
import com.cpi.cornpp.plants.form.PlantForm;
import com.cpi.cornpp.plants.services.PlantService;

/**
 * Plant Add Action
 * @author Edgar Joao
 *
 */
public class PlantAddAction extends Action{

	/** Error message name*/
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/** Fail forward */
	private final static String FAIL_FORWARD = "fail";//$NON-NLS-1$
	/** Success forward */
	private final static String SUCCESS_FORWARD = "success";//$NON-NLS-1$
	/** Success new forward */
	private final static String SUCCESS_NEW_FORWARD = "success_new";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Success add key */
	private static final String SUCCESS_ADD_LABEL = "common.add.success";//$NON-NLS-1$
	/** Plant exists key */
	private static final String VALIDATION_EXIST_MESSAGE = "label.plants.add.exist.plant";//$NON-NLS-1$
	/** Parameter name */
	private static final String SAVE_AND_NEW_PARAM = "saveandnew";//$NON-NLS-1$
	
	/**
	 * Add Plant Action
	 * @author Edgar Joao
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws PlantException {
		
		PlantService service = null;
		MessageResources messageResources = null;
		String messageError = null;				
		PlantForm plantForm;
		String forward = null;
		HttpSession httpSession = null;
		plantForm = (PlantForm) form;
		int saveAndNew = 0;
		
		try{
			saveAndNew = Integer.parseInt(request.getParameter(SAVE_AND_NEW_PARAM));
			httpSession = request.getSession(false);
			messageResources = getResources(request);
		
		if(plantForm != null){
			service = new PlantService();
			
			if(!service.existPlant(plantForm.getPlantView())){
				service.addPlant(plantForm.getPlantView());
				messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_ADD_LABEL);
				httpSession.setAttribute(MESSAGE, messageError);
				if( saveAndNew == 1 ){
					forward = SUCCESS_NEW_FORWARD;
				}else{
					forward = SUCCESS_FORWARD;
				}
			}else{				
				httpSession = request.getSession(false);
				messageResources = getResources(request);
				httpSession.setAttribute(ERROR_MESSAGE, messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), VALIDATION_EXIST_MESSAGE));
				form.reset(mapping, request);
				forward = FAIL_FORWARD;
			}			
			
		}
		
		}catch (PlantException plantException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), plantException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, plantException);
				form.reset(mapping, request);
				throw plantException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, messageError);
				forward = FAIL_FORWARD;
			}
		}
		catch (Exception e){
			PlantException plantException=null;
			e.printStackTrace(System.out);
			plantException= new PlantException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_INSERT);
			throw plantException;
		}
		form.reset(mapping, request);				
		return mapping.findForward(forward);
	}
	
}
